package com.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class ExportPdf {
	public static void main(String[] args) {
		String fileName = "test.jrxml";
		exportPdf(fileName);
	}

	public static void exportPdf(String fileName) {
		fill();
		try {
			JasperExportManager.exportReportToPdfFile("report1.jrprint", "1.pdf");
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static JRDataSource createReportDataSource() {
		JRBeanCollectionDataSource dataSource;
		Collection reportRows = initializeBeanCollection();
		dataSource = new JRBeanCollectionDataSource(reportRows);
		return dataSource;
	}

	private static Collection initializeBeanCollection() {
		List list = new ArrayList();
		for (int i = 0; i < 50; i++) {
			Reader r = new Reader();
			r.setName("rrr");
			r.setAge(i + "");
			list.add(r);
		}

		return list;
	}

	public static void fill() {
		try {

			JRDataSource je = createReportDataSource();
			String file = JasperFillManager.fillReportToFile("test.jasper", null, je);

			System.out.println("aaaa*.jrprint");
		} catch (JRException e) {
			e.printStackTrace();
		}

	}
}
